package io.kiki.sba.registry.server.clientInterface.remoting;

import io.kiki.sba.registry.api.ChannelHandler;
import io.kiki.sba.registry.api.Client;
import io.kiki.sba.registry.api.exchange.DataExchanger;
import io.kiki.sba.registry.api.exchange.RequestException;
import io.kiki.sba.registry.api.exchange.message.Request;
import io.kiki.sba.registry.api.exchange.message.Response;
import io.kiki.sba.registry.server.clientInterface.bootstrap.Config;
import io.kiki.sba.registry.server.shared.remoting.ClientSideExchanger;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Set;

/**
 * use for dataNode notify
 */
public class DataNodeNotifyExchanger extends ClientSideExchanger {
    @Autowired
    private Config config;

    @Resource(name = "dataNotifyClientHandlers")
    private Collection<ChannelHandler> channelHandlers;

    public DataNodeNotifyExchanger() {
        super(DataExchanger.DATA_SERVER_NOTIFY_TYPE);
    }


    @Override
    public Response request(Request request) throws RequestException {
        // dataNode notify unsupported request to data server
        throw new UnsupportedOperationException();
    }

    @Override
    public Client connectServer() {
        Set<String> ips = serverIpSet;
        int count = tryConnectAllServer(ips);
        if (count != ips.size()) {
            throw new RuntimeException("failed to connect all dataServers: " + ips);
        }
        return getClient();
    }

    @Override
    protected Collection<ChannelHandler> getClientHandlers() {
        return channelHandlers;
    }

    @Override
    public int getRpcTimeoutMillis() {
        return config.getDataNodeExchangeTimeoutMillis();
    }

    @Override
    public int getServerPort() {
        return config.getDataServerNotifyPort();
    }

    @Override
    public int getConnNum() {
        return config.getDataNotifyClientConnNum();
    }
}
